import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:syn_4s/core/extension/build_context_extension.dart';

import '../../ui/widgets/custom_ui.dart';
import '../model/cctv_camera_model.dart';
import '../model/short_model.dart';
import '../services/combo_request.dart';
import '../services/config.dart';
import '../services/surveillance_request.dart';
import '../utils/dialog_utils.dart';
import '../utils/toast_utils.dart';
import 'main_view_model.dart';

class SurveillanceViewModel extends GetxController{

  var currentPage = 1.obs;
  var totalPages = 10.obs;
  var sort = true.obs;
  RxList<CCTVModel> items = <CCTVModel>[].obs;

  getDevices(
      BuildContext context,
      GlobalKey globalKey,
      ) {
    getCombos(context, globalKey, typeKey: "device", callback: (data) {
      showShortMenu(context, data, globalKey).then((value) {

      });
    });
  }

  getCombos(BuildContext context, GlobalKey globalKey,
      {required String typeKey, required GetShortCallback callback}) {
    DialogUtils.loading(context);
    ComboRequest.getCombo(typeKey).then((value) {
      DialogUtils.closeDialog(context);
      if (value.status != HttpConfig.success) {
        ToastUtils.showToast(value.message ?? "");
        return;
      }
      List<ShortModel> data = value.data ?? [];
      callback(data);
    }).catchError((err) {
      DialogUtils.closeDialog(context);
      ToastUtils.showNetError(context);
    });
  }

  void getCCTV(BuildContext context) {
    SurveillanceRequest.getCCTV(currentPage.value, sort.value).then((result) {
      if (result.status == HttpConfig.success) {
        items.clear();
        // totalPages.value = result!.data!.totalPages!;
        items.addAll(result!.data!.cctv!);
      }
    }).catchError((error) {
      ToastUtils.showNetError(context);
    });
  }

  void nextPage(BuildContext context) {
    currentPage.value = (currentPage.value + 1).clamp(1, totalPages.value);
    getCCTV(context);
  }

  void prevPage(BuildContext context) {
    currentPage.value = (currentPage.value - 1).clamp(1, totalPages.value);
    getCCTV(context);
  }

  void changSort(BuildContext context){
    sort.value = !sort.value;
    getCCTV(context);
  }

}